// pages/userCenter/merchantReg/merchantReg.js
const app = getApp()
const request = require('../../../utils/request')
const regex = require('../../../utils/regex')
const utils = require('../../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardImg: '',
    shopImg: '',
    businessImg: '',
    imgHost: app.d.imgPath,
    radioItems: [
      {name: 0, value: '男', checked: 'true'},
      {name: 1, value: '女'}
    ],
    gender: '男',
    birthdate: '2015-09-01',
    clearValue: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      birthdate: utils.parseTime(new Date(), '{y}-{m}-{d}')
    })
  },

  uploadImg(cb) {
    wx.chooseImage({
      count: 1,
      success: file => {
        wx.showLoading({
          title: '正在上传'
        })
        const tempFilePaths = file.tempFilePaths
        wx.uploadFile({
          url: app.d.severUrl + 'imgUpload',
          filePath: tempFilePaths[0],
          name: 'imgData',
          success: res => {
            cb && cb(JSON.parse(res.data))
            wx.hideLoading()
          },
          fail: error => {
            wx.showToast({
              icon: 'none',
              title: error.msg
            })
          }
        })
      }
    })
  },

  uploadShopImg() {
    this.uploadImg(res => {
      this.setData({
        shopImg: res.data.image
      })
    })
  },

  uploadCardImg() {
    this.uploadImg(res => {
      this.setData({
        cardImg: res.data.image
      })
    })
  },

  uploadBusinessImg() {
    this.uploadImg(res => {
      this.setData({
        businessImg: res.data.image
      })
    })
  },

  previewImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: [e.currentTarget.dataset.url]
    })
  },

  radioChange(e) {
    this.setData({
      gender: e.detail.value
    })
  },

  bindDateChange(e) {
    this.setData({
      birthdate: e.detail.value
    })
  },

  formSubmit(e) {
    const value = e.detail.value
    
    if (!value.shop_name) {
      wx.showToast({
        icon: 'none',
        title: '请填写商家名称'
      })
      return
    }

    if (!value.shop_address) {
      wx.showToast({
        icon: 'none',
        title: '请填写商家地址'
      })
      return
    }

    if (!this.data.shopImg) {
      wx.showToast({
        icon: 'none',
        title: '请添加商家照片'
      })
      return
    }

    if (!value.realname) {
      wx.showToast({
        icon: 'none',
        title: '请填写姓名'
      })
      return
    }

    if (!this.data.gender) {
      wx.showToast({
        icon: 'none',
        title: '请选择性别'
      })
      return
    }

    if (!this.data.birthdate) {
      wx.showToast({
        icon: 'none',
        title: '请选择生日'
      })
      return
    }

    if (!value.native_place) {
      wx.showToast({
        icon: 'none',
        title: '请填写籍贯'
      })
      return
    }

    if (!value.phone) {
      wx.showToast({
        icon: 'none',
        title: '请填写手机号'
      })
      return
    }

    if (!regex.phone.test(value.phone)) {
      wx.showToast({
        icon: 'none',
        title: '手机号格式错误'
      })
      return
    }

    if (!value.wechat) {
      wx.showToast({
        icon: 'none',
        title: '请填写微信'
      })
      return
    }

    if (!this.data.cardImg) {
      wx.showToast({
        icon: 'none',
        title: '请添加身份证照片'
      })
      return
    }

    if (!this.data.businessImg) {
      wx.showToast({
        icon: 'none',
        title: '请添加营业执照'
      })
      return
    }

    if (!value.password) {
      wx.showToast({
        icon: 'none',
        title: '请填写密码'
      })
      return
    }

    if (value.password.length < 6) {
      wx.showToast({
        icon: 'none',
        title: '密码长度不能小于6位'
      })
      return
    }

    if (!value.cpassword) {
      wx.showToast({
        icon: 'none',
        title: '请确认密码'
      })
      return
    }

    if (value.password !== value.cpassword) {
      wx.showToast({
        icon: 'none',
        title: '两次填写密码不一致'
      })
      return
    }

    wx.showLoading()
    const data = {
      shop_name: value.shop_name,
      shop_address: value.shop_address,
      realname: value.realname,
      sex: this.data.gender,
      birthday: this.data.birthdate,
      native_place: value.native_place,
      phone: value.phone,
      wechat: value.wechat,
      password: value.password,
      shop_img: this.data.shopImg,
      business_img: this.data.businessImg,
      card_img: this.data.cardImg
    }
    
    request.post('shop_reg', data)
      .then(res => {
        wx.hideLoading()
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
        this.setData({
          clearValue: '',
          shopImg: '',
          businessImg: '',
          cardImg: '',
          gender: '男',
          radioItems: [
            {name: 0, value: '男', checked: 'true'},
            {name: 1, value: '女'}
          ],
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})